﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Dukou.Js
{
    public class JsNumber : IJsRender<JsNumber>
    {
        public string VariableName { get; private set; }

        public decimal VariableValue { get; private set; }

        public JsNumber(string name, int value)
            : this(name, Convert.ToDecimal(value))
        {
        }

        public JsNumber(string name, long value)
            : this(name, Convert.ToDecimal(value))
        {
        }

        public JsNumber(string name, float value)
            : this(name, Convert.ToDecimal(value))
        {
        }

        public JsNumber(string name, double value) 
            : this (name, Convert.ToDecimal(value))
        {
        }

        public JsNumber(string name, decimal value)
        {
            this.VariableName = name;
            this.VariableValue = value;
        }

        public JsNumber Render(HtmlHelper htmlHelper)
        {
            htmlHelper.ViewContext.Writer.Write(string.Format("var {0} = {1};", this.VariableName, this.VariableValue));
            htmlHelper.ViewContext.Writer.Write(Environment.NewLine);
            return this;
        }

        public static JsNumber JsEqual(HtmlHelper htmlHelper, string name, JsNumber t)
        {
            htmlHelper.ViewContext.Writer.Write(string.Format("var {0} = {1};", name, t.VariableName));
            htmlHelper.ViewContext.Writer.Write(Environment.NewLine);
            return new JsNumber(name, t.VariableValue);
        }
    }
}
